package org.feng.controller;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.feng.entity.EnumEntity;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * TODO 类的描述
 *
 * @version V1.0
 * @author: fengjinsong
 * @date: 2022年09月06日 19时13分
 */
public class PinYinController {

    private static final String UNDER_SCORE = "_";

    public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入枚举类名");
        String enumClassName = scanner.next();
        String classDefine = "public enum " + enumClassName + " {";
        String classDesc = "/**\n" +
                " * TODO 类的描述\n" +
                " *\n" +
                " * @version V1.0\n" +
                " * @author: auto\n" +
                " * @date: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分")) + "\n" +
                " */\n" + "@Getter\n@AllArgsConstructor\n" + classDefine;

        System.out.println("import lombok.AllArgsConstructor;");
        System.out.println("import lombok.Getter;");
        System.out.println();
        System.out.println(classDesc);
        List<EnumEntity> enumEntities = preHandle(args);
        generateEnumFieldName(enumEntities);
        List<String> result = EnumEntity.toEnum(enumEntities);
        result.stream().map(res -> "    " + res).forEach(System.out::println);
        System.out.println();
        System.out.println("    private final String code;\n" +"    private final String description;");
        System.out.println("}");
    }

    private static List<EnumEntity> preHandle(String[] args) {
        if (args.length == 0 || args.length % 2 != 0) {
            return Collections.emptyList();
        }
        List<EnumEntity> enumEntities = new ArrayList<>();
        for (int i = 0; i < args.length - 1; i = i + 2) {
            EnumEntity entity = new EnumEntity();
            entity.setHanYuName(args[i]);
            entity.setCode(args[i + 1]);
            enumEntities.add(entity);
        }
        return enumEntities;
    }

    /**
     * 获得枚举变量名
     *
     * @param enumEntities 枚举实体元数据列表
     */
    private static void generateEnumFieldName(List<EnumEntity> enumEntities) throws BadHanyuPinyinOutputFormatCombination {
        if (Objects.isNull(enumEntities) || enumEntities.isEmpty()) {
            return;
        }

        for (EnumEntity enumEntity : enumEntities) {
            String chineseName = enumEntity.getHanYuName();
            if (Objects.nonNull(chineseName)) {
                String pinyinString = PinyinHelper.toHanYuPinyinString(chineseName);
                String withUnderLine = pinyinString.replaceAll("[1-4]", UNDER_SCORE);
                int length = withUnderLine.length();

                String preEnumName = withUnderLine.substring(0, length - 1).toUpperCase(Locale.ROOT);
                enumEntity.setEnumName(replaceSpecialChar(preEnumName));
            }
        }
    }

    private static String replaceSpecialChar(String preEnumName) {
        preEnumName = preEnumName.replaceAll("\\(", UNDER_SCORE);
        preEnumName = preEnumName.replaceAll("\\)", UNDER_SCORE);
        preEnumName = preEnumName.replaceAll("/", UNDER_SCORE);
        String doubleUnderScore = "__";
        while (preEnumName.contains(doubleUnderScore)) {
            preEnumName = preEnumName.replaceAll(doubleUnderScore, UNDER_SCORE);
        }

        if (preEnumName.endsWith(UNDER_SCORE)) {
            preEnumName = preEnumName.substring(0, preEnumName.length() - 1);
        }
        return preEnumName;
    }
}
